			package ServletHandle;

			import java.io.IOException;
 import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

			import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAL.Rank;

import java.text.SimpleDateFormat;
import java.util.*;

			public class ScoreHandle extends HttpServlet {

			HttpServletRequest request;
			HttpServletResponse response;
			DAL.Score score=new DAL.Score();
			DAL.Math math=new DAL.Math();
	
			//通过表单get方式传值 将进入doGet函数（method="get"）
			public void doGet(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
					this.response=response;
					this.request=request;
					int handleType=Integer.parseInt(request.getParameter("type").toString());
					switch (handleType) {
					case 1://类型1代表删除表中的数据
						deleteEntity();
						break;
					case 4://类型4代表获取表中信息
						getEntity();
						break;
					case 5://类型5代表根据查询条件获取表中信息
						getEntityByWhere();
						break;
					case 6://类型6代表用户查询比赛成绩
						getEntityOrderBy();
						break;
					case 7://类型7代表获取表中信息
						searchEntity();
						break;
					case 8://类型8代表用户查询比赛成绩
						searchEntityByWhere();
						break;
					case 9://类型8代表用户查询比赛成绩
						getEntityPlayerCategoryChart();
						break;
					default:
						break;
					}
			}
	
			//通过表单post方式传值 将进入doPost函数（method="post"）
			public void doPost(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
					this.request=request;
					this.response=response;
					int handleType=Integer.parseInt(request.getParameter("type").toString());//将前台页面传过来的type类型转化成整型
					switch (handleType) {
					case 2://类型2代表更新表中的数据
						updateEntity();
						break;
					case 3://类型3代表向表中添加数据
						insertEntity();
						break;
					default:
						break;
					}
			}

			private void getEntityOrderBy() throws ServletException, IOException
			{
				request.setCharacterEncoding("UTF-8");
				String condition=request.getParameter("condition");//获取查询字段的名称
				String value=new String(request.getParameter("value").getBytes("ISO8859_1"),"UTF-8");//获取查询的值
				String rankby=new String(request.getParameter("rankby").getBytes("ISO8859_1"),"UTF-8");//获取查询的值
				String where=condition+"=\""+value+"\"";//拼接查询字符串				
				List<Object> list=score.getEntityOrderBy(where, rankby);
				request.setAttribute("list",list);//将数据存放到request对象中，用于转发给前台页面使用
				request.getRequestDispatcher("/Admin/ScoreSearch.jsp").forward(request, response);//请求转发
			}
			
			//删除数据操作
			private void deleteEntity() throws IOException
			{
				String ScoreId=request.getParameter("ScoreId");//获取前台通过get方式传过来的JId
				score.deleteEntity(ScoreId);//执行删除操作
				response.sendRedirect("/SPORT/ScoreHandle?type=4");//删除成功后跳转至管理页面
			}

			//更新数据操作
			private void updateEntity() throws UnsupportedEncodingException
			{
				String ScoreId=new String(request.getParameter("ScoreId").getBytes("ISO8859_1"),"UTF-8");
				String SchId=new String(request.getParameter("SchId").getBytes("ISO8859_1"),"UTF-8");
				String mark=new String(request.getParameter("mark").getBytes("ISO8859_1"),"UTF-8");
				String grade=new String(request.getParameter("grade").getBytes("ISO8859_1"),"UTF-8");

				if(score.updateEntity(ScoreId,SchId,mark,grade)==1)
				{
					List<Object> objList=score.getEntityById(ScoreId);
					Object[] obj=(Object[])objList.get(0);
					String MathId=obj[4].toString();//获取比赛项目编号
					String MathTag=obj[11].toString();//获取比赛项目排名表示
					Rank rank=new Rank();
					rank.deleteItem(MathId);//删除最高分数据
					List<Object> scoreList=score.getTop(MathId, MathTag);
					Object[] objScore=(Object[])scoreList.get(0);
					SimpleDateFormat dateFormat =new    SimpleDateFormat("yyyyMMddHHmmss"); 
					String rankId=dateFormat.format(new Date());
					String mathId2=objScore[4].toString();
					String playerId2=objScore[3].toString();
					String score=objScore[1].toString();
					rank.insertEntity(rankId, mathId2, playerId2, score);
					
					try {
						response.sendRedirect("/SPORT/ScoreHandle?type=4");//成功更新数据后跳转至ScoreMsg.jsp页面
					} catch (IOException e) {
						e.printStackTrace();//异常处理
					}
				}
			}

			//插入数据操作
			private void insertEntity() throws UnsupportedEncodingException, IOException
			{
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=UTF-8");
				PrintWriter out=response.getWriter();
				
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
				String ScoreId=dateFormat.format(new Date());
				String SchId=new String(request.getParameter("SchId").getBytes("ISO8859_1"),"UTF-8");
				String mark=new String(request.getParameter("mark").getBytes("ISO8859_1"),"UTF-8");
				String grade=new String(request.getParameter("grade").getBytes("ISO8859_1"),"UTF-8");

				if(!score.checkExist(ScoreId))
				{
					if(score.insertEntity(ScoreId,SchId,mark,grade)==1)
					{
						out.write("<script>alert('数据添加成功！'); location.href = '/SPORT/ScoreHandle?type=4';</script>");
					}
					else {
						out.write("<script>alert('数据添失败！'); location.href = '/SPORT/ScoreHandle?type=4';</script>");
					}
				}
				else {
					out.write("<script>alert('主键重复，数据添加失败！'); location.href = '/SPORT/ScoreHandle?type=4';</script>");
				}
			}

			//获取对象所有数据列表
			private void getEntity() throws ServletException, IOException
			{
				request.setCharacterEncoding("UTF-8");
				int page=request.getParameter("page")==null?1:Integer.parseInt(request.getParameter("page").toString());//获取跳转的页面号
				int totalPage=Integer.parseInt(score.getPageCount().toString()) ;//获取分页总数
				List<Object> list=score.getEntity(page);//获取数据列表
				request.setAttribute("list",list);//将数据存放到request对象中，用于转发给前台页面使用
				request.setAttribute("totalPage",totalPage );//将totalPage存放到request对象中，用于转发给前台页面使用
				String type=request.getParameter("type");//将前台页面传过来的type类型转化成整型
				if(type.equals("4"))
				{
					request.getRequestDispatcher("/Admin/ScoreMsg.jsp").forward(request, response);//请求转发
				}
				else {
					request.getRequestDispatcher("/Admin/ScoreSearch.jsp").forward(request, response);//请求转发
				}
			}
			private void getEntityPlayerCategoryChart() throws ServletException, IOException
			{
				request.setCharacterEncoding("UTF-8");
				int page=request.getParameter("page")==null?1:Integer.parseInt(request.getParameter("page").toString());//获取跳转的页面号
				int totalPage=Integer.parseInt(math.getPageCount().toString()) ;//获取分页总数
				List<Object> list2=score.getEntity(page);//获取数据列表
				List<Object> list=math.getEntity(page);//获取数据列表
				request.setAttribute("list",list);//将数据存放到request对象中，用于转发给前台页面使用
				request.setAttribute("totalPage",totalPage );//将totalPage存放到request对象中，用于转发给前台页面使用
				request.getRequestDispatcher("/Admin/MathMsg.jsp").forward(request, response);//请求转发
				
			
				
				CHART.PlayerChart.category(list2);
				
			}
			
			//获取对象所有数据列表
			private void searchEntity() throws ServletException, IOException
			{
				request.setCharacterEncoding("UTF-8");
				int page=request.getParameter("page")==null?1:Integer.parseInt(request.getParameter("page").toString());//获取跳转的页面号
				int totalPage=Integer.parseInt(score.getPageCount().toString()) ;//获取分页总数
				List<Object> list=score.getEntity(page);//获取数据列表
				request.setAttribute("list",list);//将数据存放到request对象中，用于转发给前台页面使用
				request.setAttribute("totalPage",totalPage );//将totalPage存放到request对象中，用于转发给前台页面使用
				String type=request.getParameter("type");//将前台页面传过来的type类型转化成整型
			
				request.getRequestDispatcher("/Admin/ScoreDetail.jsp").forward(request, response);//请求转发
				
			}
			
			//根据查询条件获取对象所有数据列表
			private void getEntityByWhere() throws ServletException, IOException
			{
				request.setCharacterEncoding("UTF-8");
				String condition=request.getParameter("condition");//获取查询字段的名称
				String value=new String(request.getParameter("value").getBytes("ISO8859_1"),"UTF-8");//获取查询的值
				String where=condition+"=\""+value+"\"";//拼接查询字符串
				int page=request.getParameter("page")==null?1:Integer.parseInt(request.getParameter("page"));//获取要跳转的页面号
				int wherePage=Integer.parseInt(score.getPageCountByWhere(where).toString()) ;//获取查询后的分页总数
				List<Object> list=score.getEntityByWhere(where, page);//获取查询后的数据列表
				request.setAttribute("list",list);//将数据存放到request对象中，用于转发给前台页面使用
				request.setAttribute("wherePage",wherePage );
				request.setAttribute("condition",condition);
				request.setAttribute("value",value);
				String type=request.getParameter("type");//将前台页面传过来的type类型转化成整型
				if(type.equals("5"))
				{
					request.getRequestDispatcher("/Admin/ScoreMsg.jsp").forward(request, response);
				}
				else {
					request.getRequestDispatcher("/Admin/ScoreSearch.jsp").forward(request, response);//请求转发
				}
				
			}
			
			//根据查询条件获取对象所有数据列表
			private void searchEntityByWhere() throws ServletException, IOException
			{
				request.setCharacterEncoding("UTF-8");
				String condition=request.getParameter("condition");//获取查询字段的名称
				String value=new String(request.getParameter("value").getBytes("ISO8859_1"),"UTF-8");//获取查询的值
				String where=condition+"=\""+value+"\"";//拼接查询字符串
				int page=request.getParameter("page")==null?1:Integer.parseInt(request.getParameter("page"));//获取要跳转的页面号
				int wherePage=Integer.parseInt(score.getPageCountByWhere(where).toString()) ;//获取查询后的分页总数
				List<Object> list=score.getEntityByWhere(where, page);//获取查询后的数据列表
				request.setAttribute("list",list);//将数据存放到request对象中，用于转发给前台页面使用
				request.setAttribute("wherePage",wherePage );
				request.setAttribute("condition",condition);
				request.setAttribute("value",value);
				request.getRequestDispatcher("/Admin/ScoreDetail.jsp").forward(request, response);
			
				
			}
		}
